package org.example.lys.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;

/**
 * @author ; lidongdong
 * @Description 日期工具类
 * @Date 2019-09-20
 */
@Slf4j
public class DateUtils {

    public static final int DAY = 1;
    public static final int HOUR = 2;
    public static final int MIN = 3;
    public static final int SEC = 4;
    public static final String yyyy = "yyyy";
    public static final String MM = "MM";
    public static final String dd = "dd";
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String MMDDHHMMSS = "MMddHHmmss";
    public static final String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_ddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyyMMdd2 = "yyyy 年 MM 月 dd 日";
    public static final String yyyyMMdd3 = "yyyy年MM月dd日";
    public static final String yyyy_MM = "yyyy-MM";

    public static final String ZERO = " 00:00:00";
    public static final String DAY_END = " 23:59:59";

    public DateUtils() {
    }

    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) - day);
        return now.getTime();
    }

    public static Date getDateBeforeHour(Date d, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(11, calendar.get(11) - hour);
        new SimpleDateFormat(yyyy_MM_ddHHmmss);
        return calendar.getTime();
    }

    public static Date addDateMinut(Date date, int x) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(12, x);
        date = cal.getTime();
        cal = null;
        return date;
    }

    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) + day);
        return now.getTime();
    }

    public static String getDateFormatter() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(yyyy_MM_ddHHmmss);
        return df.format(date);
    }

    public static String format(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    public static String getDate() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(yyyyMMdd);
        return df.format(date);
    }

    public static String formatDate(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    public static Date parse(String dateStr, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = null;

        try {
            date = df.parse(dateStr);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return date;
    }

    public static long getDateDifference(Date startTime, Date endTime, int type) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long ns = 1000L;
        long diff = endTime.getTime() - startTime.getTime();
        long day = 0L - diff / nd;
        long hour = 0L - diff % nd / nh;
        long min = 0L - diff % nd % nh / nm;
        long sec = 0L - diff % nd % nh % nm / ns;
        long result = 0L;
        switch (type) {
            case 1:
                result = day;
                break;
            case 2:
                result = hour;
                break;
            case 3:
                result = min;
                break;
            case 4:
                result = sec;
        }

        return result;
    }

    public static String getDateDiffNowInSimple(Date endTime) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long ns = 1000L;
        long diff = endTime.getTime() - (new Date()).getTime();
        long day = 0L - diff / nd;
        long hour = 0L - diff % nd / nh;
        long min = 0L - diff % nd % nh / nm;
        long sec = 0L - diff % nd % nh % nm / ns;
        if (day > 0L) {
            if (day <= 30L) {
                if (day == 1L) {
                    return "昨天";
                }

                if (day >= 7L) {
                    long weeks = day / 7L;
                    return weeks + "周前";
                }

                return day + "天前";
            }
        } else {
            if (hour > 0L) {
                return hour + "小时前";
            }

            if (min > 0L) {
                return min + "分钟前";
            }

            if (sec > 0L) {
                return sec + "秒前";
            }
        }

        return formatDate(endTime, "yyyy-MM-dd HH:mm:ss");
    }

    public static boolean isAtNight() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        String hour = sdf.format(new Date());
        int k = Integer.parseInt(hour);
        return k >= 0 && k < 6 || k >= 18 && k < 24;
    }

    public static boolean isBetween(Time startTime, Time endTime) {
        long now = Time.valueOf(formatDate(new Date(), "HH:mm:ss")).getTime() + 28800000L;
        long start = startTime.getTime() + 28800000L;
        long end = endTime.getTime() + 28800000L;
        return start < end ? now > start && now < end : (startTime.getTime() <= endTime.getTime() ? true : start >= 0L && start <= now || end >= now && end <= 86400000L);
    }

    public static String getBusinessStartDate(int hour) {
        Calendar c = Calendar.getInstance();
        c.set(11, hour);
        c.set(12, 0);
        c.set(13, 0);
        c.set(5, c.get(5) - 1);
        Date m6 = c.getTime();
        return formatDate(m6, yyyy_MM_ddHHmmss);
    }

    public static String getBusinessEndDate(int hour) {
        Calendar c = Calendar.getInstance();
        c.set(11, hour);
        c.set(12, 0);
        c.set(13, 0);
        c.set(5, c.get(5));
        Date m6 = c.getTime();
        return formatDate(m6, yyyy_MM_ddHHmmss);
    }

    public static int getField(Date date, int field) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        return ca.get(field);
    }

    public static Date formatDate(String strDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        try {
            return sdf.parse(strDate);
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String checkDay(Date date) {
        date.setMinutes(0);
        date.setSeconds(0);
        SimpleDateFormat df = new SimpleDateFormat(yyyy_MM_ddHHmmss);
        String today = df.format(date);
        return today;
    }

    public static int daysBetween(String maxTime, String minTime) {
        Date max = parse(maxTime, yyyy_MM_ddHHmm);
        Date min = parse(minTime, yyyy_MM_ddHHmm);
        return daysBetween(max, min);
    }

    public static int daysBetween(Date maxTime, Date minTime) {
        long max = maxTime.getTime();
        long min = minTime.getTime();
        Long day = Long.valueOf((max - min) / 86400000L);
        return day.intValue();
    }

    /**
     * 获取当前系统时间
     * 返回 yyyy-mm-dd
     *
     * @return
     */
    public static String getDateStr(Date date) {
        return formatDate(date, yyyy_MM_dd);
    }

    public static Date addDays(Date date, int val) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        cal.setTime(date);
        cal.add(6, val);
        return cal.getTime();
    }


    /**
     * 获取今天剩余毫秒数
     *
     * @return
     */
    public static long fetchTodayEndMillis() {
        long now = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        long next = cal.getTime().getTime();
        return next - now;
    }

    /**
     * 取得当月天数
     */
    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 取得当月天数
     */
    public static int fetchToday() {
        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * 获取本月剩余毫秒数
     *
     * @return
     */

    public static Long fetchToMonthEndMillis() {
        //获取毫秒数
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return getMonthEndTime(milliSecond, "GMT+8:00") - milliSecond;
    }

    /**
     * 获取当月的结束时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @param timeZone  如 GMT+8:00
     * @return
     */
    public static Long getMonthEndTime(Long timeStamp, String timeZone) {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 获取当前月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }


    /**
     * 获取本年剩余毫秒数
     *
     * @return
     */

    public static Long fetchToYearEndMillis() {
        //获取毫秒数
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return getYearEndTime(milliSecond) - milliSecond;
    }


    /**
     * 获取当月的结束时间戳
     *
     * @param milliSecond 获取毫秒数
     * @return
     */
    public static Long getYearEndTime(Long milliSecond) {
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTimeInMillis(milliSecond);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.MONTH, 0);
        // 获取当前月最后一天
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 毫秒值格式化
     *
     * @param nowTimeMillisecond
     * @return
     */
    public static String timeMillisecondFormat(Long nowTimeMillisecond) {
        Date date = new Date();
        date.setTime(nowTimeMillisecond);
        return new SimpleDateFormat(yyyy_MM_ddHHmmss).format(date);
    }


    /**
     * 获取开始时间
     *
     * @param beginDate tag:2019-07-20
     * @return
     */
    public static Date getBeginDateTime(String beginDate) {
        String s = "/";
        if (beginDate.indexOf(s) != -1) {
            beginDate = beginDate.replaceAll(s, "-");
            String[] e = beginDate.split("\\-");

            int e1;
            int i;
            for (e1 = 1; e1 <= 9; ++e1) {
                i = Integer.parseInt(e[1]);
                if (e1 == i) {
                    e[1] = "0" + String.valueOf(e1);
                    break;
                }
            }

            for (e1 = 1; e1 <= 9; ++e1) {
                i = Integer.parseInt(e[2]);
                if (e1 == i) {
                    e[2] = "0" + String.valueOf(e1);
                    break;
                }
            }

            beginDate = e[0] + "-" + e[1] + "-" + e[2];
        }
        Date beginDateTime = null;
        try {
            beginDateTime = new SimpleDateFormat(yyyy_MM_ddHHmmss).parse(beginDate.substring(0, 10) + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return beginDateTime;
    }

    /**
     * 获取结束时间
     *
     * @param endDate tag:2019-07-20
     * @return
     */
    public static Date getEndDateTime(String endDate) {
        String s = "/";
        if (endDate.indexOf(s) != -1) {
            endDate = endDate.replaceAll(s, "-");
            String[] endDateTime = endDate.split("\\-");
            for (int e = 1; e <= 9; ++e) {
                int i = Integer.parseInt(endDateTime[1]);
                if (e == i) {
                    endDateTime[1] = "0" + String.valueOf(e);
                    break;
                }
            }
            for (int e = 1; e <= 9; ++e) {
                int i = Integer.parseInt(endDateTime[2]);
                if (e == i) {
                    endDateTime[2] = "0" + String.valueOf(e);
                    break;
                }
            }
            endDate = endDateTime[0] + "-" + endDateTime[1] + "-" + endDateTime[2];
        }
        Date endDateTime = null;
        try {
            endDateTime = new SimpleDateFormat(yyyy_MM_ddHHmmss).parse(endDate.substring(0, 10) + " 23:59:59");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDateTime;
    }

    /**
     * 获取开始时间
     *
     * @param endDate 时间 yyyy-mm-dd
     * @param format  日期格式 yyyy-mm-dd
     * @return
     */
    public static Date getBeginDateTime(String endDate, String format) {
        Date date = formatDate(endDate, format);
        return getBeginDateTime(date);
    }

    /**
     * 获取结束时间
     *
     * @param endDate 时间 yyyy-mm-dd
     * @param format  日期格式 yyyy-mm-dd
     * @return
     */
    public static Date getEndDateTime(String endDate, String format) {
        Date date = formatDate(endDate, format);
        return getEndDateTime(date);
    }

    /**
     * 获取开始时间
     * 获得某天最小时间 2017-10-15 00:00:00
     *
     * @param beginDate tag:2019-07-20
     * @return
     */
    public static Date getBeginDateTime(Date beginDate) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(beginDate.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取结束时间
     * 获得某天最大时间 2017-10-15 23:59:59
     *
     * @param endDate
     * @return
     */
    public static Date getEndDateTime(Date endDate) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(endDate.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 判断是否跨月
     *
     * @param date
     * @return
     */
    public static Boolean isSameMonth(Date date) {

        return Integer.valueOf(new SimpleDateFormat("MM").format(new Date())) > Integer.valueOf(new SimpleDateFormat("MM").format(date));

    }

    /**
     * 判断两个日期跨度是否超过某一个天数
     *
     * @param startDate 开始时间 yyyy-MM-dd
     * @param endDate   结束时间 yyyy-MM-dd
     * @param day       相差天数
     * @return true = 超出最大查询天数, false = 未超出最大查询天数
     */
    public static Boolean checkFindTimeSpanDay(String startDate, String endDate, Integer day) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_ddHHmmss);
            Date timeStart = simpleDateFormat.parse(startDate.substring(0, 10) + " 00:00:00");
            Date timeEnd = simpleDateFormat.parse(endDate.substring(0, 10) + " 23:59:59");
            return timeEnd.getTime() - timeStart.getTime() > day * 24 * 60 * 60 * 1000L;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断两个日期跨度是否超过某一个天数
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param day       相差天数
     * @return true = 超出最大查询天数, false = 未超出最大查询天数
     */
    public static Boolean checkFindTimeSpanDay(Date startDate, Date endDate, Integer day) {
        return checkFindTimeSpanDay(formatDate(startDate, yyyy_MM_ddHHmmss), formatDate(endDate, yyyy_MM_ddHHmmss), day);
    }

    /**
     * 判断当前时间是否超过某天
     * 是否超过25号
     * 2021-09-10，改为当前时间大于等于25号
     *
     * @param day 日期
     * @return false =大于等于，true =小于
     */
    public static Boolean checkMoreThanDay(Integer day) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dd);
        try {
            return Integer.valueOf(simpleDateFormat.format(new Date())) >= Integer.valueOf(simpleDateFormat.format(simpleDateFormat.parse(String.valueOf(day))));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 当前日期的后day天
     *
     * @param day
     * @return
     */
    public static String getCurDateAfter(Integer day) {
        SimpleDateFormat sf = new SimpleDateFormat(yyyy_MM_dd);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, day);
        return sf.format(c.getTime());
    }

    /**
     * 根据年龄计算出生年
     *
     * @param birthDay
     * @return
     */
    public static Integer getBirth(Integer birthDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return Integer.parseInt(sdf.format(new Date())) - birthDay;
    }

    /**
     * 计算n年之后或之前的日期
     *
     * @param date 当前时间
     * @param val  n
     * @return
     */
    public static Date addYears(Date date, int val) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.add(Calendar.YEAR, val);
        return cal.getTime();
    }

    /**
     * 计算n月之后或之前的日期
     *
     * @param date
     * @param val
     * @return
     */
    public static Date addMonth(Date date, int val) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.add(Calendar.MONTH, val);
        return cal.getTime();
    }

    /**
     * 时间加 val分钟
     *
     * @param date
     * @param val
     * @return
     */
    public static Date addMinute(Date date, int val) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, val);
        return cal.getTime();
    }

    /**
     * 获得当前月--开始日期
     *
     * @param date
     * @return
     */
    public static String getMinMonthDate(String date) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(yyyy_MM);
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMdd);
        try {
            calendar.setTime(format.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 获得当前月--结束日期
     *
     * @param date
     * @return
     */
    public static String getMaxMonthDate(String date) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(yyyy_MM);
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy_MM_dd);
        try {
            calendar.setTime(format.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 获得当前月--开始日期
     *
     * @param date         时间；例如：2023-01
     * @param outPutFormat 格式
     * @return 2023-01-01
     */
    public static String getMinMonthDateByFormat(String date, String outPutFormat) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(yyyy_MM);
        SimpleDateFormat dateFormat = new SimpleDateFormat(outPutFormat);
        try {
            calendar.setTime(format.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 获得当前月--结束日期
     *
     * @param date         时间；例如：2023-01
     * @param outPutFormat 格式
     * @return 2023-01-01
     */
    public static String getMaxMonthDateByFormat(String date, String outPutFormat) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat(yyyy_MM);
        SimpleDateFormat dateFormat = new SimpleDateFormat(outPutFormat);
        try {
            calendar.setTime(format.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 比较开始时间是否小于结束时间
     *
     * @param beginDate 开始时间
     * @param bndDate   结束时间
     * @return true=beginDate小于bndDate  false=beginDate大于等于bndDate
     */
    public static Boolean checkBeginDateBeforeEndDate(String beginDate, String bndDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy_MM_dd);
        try {
            return dateFormat.parse(beginDate).before(dateFormat.parse(bndDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 比较开始时间和结束时间是否超过val年
     *
     * @param beginDate 开始时间
     * @param bndDate   结束时间
     * @param val       超过的年数
     * @return true=beginDate到bndDate跨越val年  false=beginDate到bndDate没有跨越val年
     */
    public static Boolean checkBeginDateBeforeEndDate(String beginDate, String bndDate, int val) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy_MM_dd);
        try {

            return addYears(dateFormat.parse(beginDate), 1).before(dateFormat.parse(bndDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 比较开始时间和结束时间是否超过val年
     *
     * @param beginDate 开始时间
     * @param bndDate   结束时间
     * @param val       超过的年数
     * @return
     */
    public static Integer checkBeginDateCompareToEndDate(String beginDate, String bndDate, int val) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy_MM_dd);
        try {

            return addYears(dateFormat.parse(beginDate), 1).compareTo(dateFormat.parse(bndDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        } else {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }

    /**
     * 得到月度开始
     * -- 根据输入得到月份
     *
     * @param amount 量
     * @return {@link Date}
     */
    public static Date getLastMonthBegin(int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.add(Calendar.MONTH, amount);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1, 0, 0, 0);
        return calendar.getTime();
    }

    /**
     * 获取历史年月日
     *
     * @param offset 偏移量
     * @return yyyyMMdd
     */
    public static String getOriginDateString(int offset) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -offset);
        return new SimpleDateFormat(yyyyMMdd).format(calendar.getTime());
    }

//    public static void main(String[] args) {
//    }

    /**
     * 获取指定年月中第一天
     *
     * @param yearMonth 年月；2022-01 或 202201
     * @return 2022-01-01
     */
    public static String getFirstDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.substring(0, 4));  //年
        int month;
        if (yearMonth.contains("-")) {
            month = Integer.parseInt(yearMonth.substring(5, 7)); //月
        } else {
            month = Integer.parseInt(yearMonth.substring(4, 6)); //月
        }
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定年月中最后一天
     *
     * @param yearMonth 年月；2022-01 或 202201
     * @return 2022-01-31
     */
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.substring(0, 4));  //年
        int month;
        if (yearMonth.contains("-")) {
            month = Integer.parseInt(yearMonth.substring(5, 7)); //月
        } else {
            month = Integer.parseInt(yearMonth.substring(4, 6)); //月
        }
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取上个月当天日期
     *
     * @param nowDate 当前日期
     * @param formate 格式
     * @return 上个月时间
     */
    public static String getLastMonthDate(String nowDate,String formate){
        Calendar cal = Calendar.getInstance();
        //获取上个月的日期（上个月的今天）
        cal.setTime(Objects.requireNonNull(formatDate(nowDate, formate)));
        cal.add(Calendar.MONTH, -1);
        Date date3 = cal.getTime();
        SimpleDateFormat format3= new SimpleDateFormat(formate);
        return format3.format(date3);
    }

}
